/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.presentation.customer;

/**
 *
 * @author Wei Liu
 */
import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import javax.swing.table.*;
import ticketmaster.dataobject.TicketView;
import ticketmaster.presentation.controls.CheckCellRenderer;
import ticketmaster.presentation.controls.ColumnData;
import ticketmaster.presentation.controls.MultiLineCellRenderer;

public class TicketsTable extends JPanel
{

    private JTable m_table;
    private TicketsTableData m_data;

    public TicketsTable(Collection<TicketView> tickets)
    {
        setSize(700, 400);

        m_data = new TicketsTableData(tickets);

        //getContentPane().add(m_title, BorderLayout.NORTH);

        m_table = new JTable();
        m_table.setAutoCreateColumnsFromModel(false);
        m_table.setModel(m_data);
        m_table.setPreferredScrollableViewportSize(new Dimension(700, 400));
        m_table.setFillsViewportHeight(true);
        m_table.setRowHeight(100);
        m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        m_table.setFont(Font.getFont("Tahoma", new Font(Font.SANS_SERIF, Font.PLAIN, 14)));
        m_table.setRowSelectionAllowed(true);
        m_table.setShowVerticalLines(false);
        //m_table.setShowGrid(false);
        //m_table.setDefaultRenderer(String.class, new MultiLineCellRenderer());

        for (int k = 0; k < TicketsTableData.columns.length; k++)
        {
            TableCellRenderer renderer;
            if (k == TicketsTableData.COL_SELECTED)
            {
                renderer = new CheckCellRenderer();
            }
            else
            {
                if (k == 0 || k == 1)
                {
                    MultiLineCellRenderer textRenderer = new MultiLineCellRenderer();
                    renderer = textRenderer;
                }
                else
                {
                    DefaultTableCellRenderer textRenderer =
                                             new DefaultTableCellRenderer();
                    textRenderer.setHorizontalAlignment(
                            TicketsTableData.columns[k].getAlignment());
                    renderer = textRenderer;
                }

                //renderer = textRenderer;
            }

            TableCellEditor editor;

            if (k == TicketsTableData.COL_SELECTED)
            {
                JCheckBox checkBox = new JCheckBox();
                checkBox.setHorizontalAlignment(JLabel.CENTER);
                editor = new DefaultCellEditor(checkBox);
            }
            else
            {
                editor = new DefaultCellEditor(new JTextField());
            }

            TableColumn column = new TableColumn(k,
                                                 TicketsTableData.columns[k].getWidth(),
                                                 renderer, editor);
            m_table.addColumn(column);
        }

        JTableHeader header = m_table.getTableHeader();
        header.setUpdateTableInRealTime(false);

        JScrollPane ps = new JScrollPane();
        ps.getViewport().add(m_table);
        ps.setSize(700, 400);

        this.add(ps);
    }

    /**
     * @return the m_table
     */
    public JTable getTable()
    {
        return m_table;
    }

    /**
     * @param m_table the m_table to set
     */
    public void setTable(JTable m_table)
    {
        this.m_table = m_table;
    }
}

class TicketsTableData extends AbstractTableModel
{

    public static final int COL_SELECTED = 6;
    static final public ColumnData columns[] =
    {
        new ColumnData("Event", 200, JLabel.LEFT),
        new ColumnData("Venue", 100, JLabel.LEFT),
        new ColumnData("Ticket Rate", 80, JLabel.CENTER),
        new ColumnData("Level", 80, JLabel.CENTER),
        new ColumnData("Seat Number", 80, JLabel.CENTER),
        new ColumnData("Price", 80, JLabel.CENTER),
        new ColumnData("Paid", 80, JLabel.CENTER)
    };
    protected SimpleDateFormat m_frm;
    private Vector m_vector;

    public TicketsTableData(Collection<TicketView> events)
    {
        m_frm = new SimpleDateFormat("MM/dd/yyyy");
        m_vector = new Vector();
        setData(events);
    }

    public void setData(Collection<TicketView> events)
    {
        m_vector.removeAllElements();
        m_vector.addAll(events);
    }

    public int getRowCount()
    {
        return m_vector == null ? 0 : m_vector.size();
    }

    public int getColumnCount()
    {
        return columns.length;
    }

    public String getColumnName(int column)
    {
        return columns[column].getTitle();
    }

    public Class getColumnClass(int c)
    {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int nRow, int nCol)
    {
        if (nCol < 6)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public Object getValueAt(int nRow, int nCol)
    {
        if (nRow < 0 || nRow >= getRowCount())
        {
            return "";
        }
        TicketView row = (TicketView) m_vector.elementAt(nRow);
        switch (nCol)
        {
            case 0:
                return row.getDisplayEventName();
            case 1:
                return row.getVenueName();
            case 2:
                return row.getSeatCategory();
            case 3:
                return row.getLevel();
            case 4:
                return row;
            case 5:
                return row.getDisplayPrice();
            case 6:
                return row.isIsPaid();
        }
        return "";
    }

    public void setValueAt(Object value, int nRow, int nCol)
    {
        if (nRow < 0 || nRow >= getRowCount())
        {
            return;
        }
        TicketView row = (TicketView) m_vector.elementAt(nRow);

        switch (nCol)
        {
            case COL_SELECTED:
                row.setIsPaid((Boolean) value);
                break;
        }
    }
}


